package com.example.personalizededucationsystem.controller;

import com.example.personalizededucationsystem.dto.request.*;
import com.example.personalizededucationsystem.dto.response.AuthResponse;
import com.example.personalizededucationsystem.dto.response.EmailSendResponse;
import com.example.personalizededucationsystem.dto.response.UpdateProfileResponse;
import com.example.personalizededucationsystem.dto.response.UserListResponse;
import com.example.personalizededucationsystem.service.AuthService;
import com.example.personalizededucationsystem.utils.IP_VALUE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = IP_VALUE.IP)
public class AuthController {

    private final AuthService authService;

    @Autowired
    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    @PostMapping("/register/request")
    public ResponseEntity<Void> registerAccount(@RequestBody EmailRequest request) {
        authService.sendEmailCode(request);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/register/request/check")
    public ResponseEntity<Boolean> checkIfUsernameAlreadyExists(@RequestBody UsernameCheckRequest username) {
        return ResponseEntity.ok(authService.CheckIfUserNameExists(username));
    }

    @PostMapping("/register/verify")
    public ResponseEntity<AuthResponse> registerAccountVerify(@RequestBody RegisterRequest request) {
        return ResponseEntity.ok(authService.verifyRegistration(request));
    }


    @PostMapping("/login")
    public ResponseEntity<AuthResponse> login(@RequestBody LoginRequest request) {
        return ResponseEntity.ok(authService.login(request));
    }

    @GetMapping("/test")
    public ResponseEntity<String> testEndpoint() {
        return ResponseEntity.ok("Hello, you are authenticated!");
    }

    @PostMapping("/find-account")
    public ResponseEntity<EmailSendResponse> sendEmail(@RequestBody EmailRequest request) {
        return ResponseEntity.ok(authService.findAccount(request));
    }

    @PostMapping("/find-account/account")
    public ResponseEntity<UserListResponse> findPreciseAccount(@RequestBody AccountTokenLinkRequest request) {
        return ResponseEntity.ok(authService.getUsernamesByToken(request));
    }

    @PutMapping("/find-account/reset-password")
    public ResponseEntity<UpdateProfileResponse> updateEmailVerification(@RequestBody ResetPasswordByTokenRequest request) {
        return ResponseEntity.ok(authService.resetPasswordByTokenAndUsername(request));
    }

}
